package com.woniuxy.ptapp.common.config;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import java.io.InputStream;

@Data
@Configuration
@ConfigurationProperties(prefix = "aliyun.oss")
public class OssConfig {

    private String endpoint;

    private String accessKeyId;

    private String accessKeySecret;

    private String bucketName;

    public  String fileUpload(String name, InputStream file){
        OSS ossClient = new OSSClientBuilder().build("https://"+endpoint,accessKeyId,accessKeySecret);

        String url = null;
        try {
            ossClient.putObject(bucketName,name,file);
            url="https://"+bucketName+"."+endpoint+"/"+name;
        } finally {
            if(ossClient!=null) ossClient.shutdown();
        }
        return url;
    }


    public  boolean deleteFile(String name){
        OSS ossClient = new OSSClientBuilder().build("https://"+endpoint,accessKeyId,accessKeySecret);
        try {
            ossClient.deleteObject(bucketName,name);
        } finally {
            if(ossClient!=null) ossClient.shutdown();
        }
        return true;
    }



}
